package dao.impl.comm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.comm.IStudioDAO;
import vo.comm.Studio;

public class StudioDAOImpl implements IStudioDAO{
	private Connection conn=null;
	private PreparedStatement pstmt=null;
	public StudioDAOImpl(Connection conn){
		this.conn=conn;
	}
	public boolean doJoin(Studio studio) throws SQLException{
		boolean flag=false;
		String sql="INSERT INTO studio(userid,username)VALUES(?,?)";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, studio.getUserid());
		this.pstmt.setString(2, studio.getUsername());
		if(this.pstmt.executeUpdate()>0){
			flag=true;
		}
		this.pstmt.close();
		return flag;
	}
	public Studio findById(String userid) throws SQLException{
		Studio studio=null;
		String sql="SELECT userid FROM studio WHERE userid=?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1,userid);
		ResultSet rs=this.pstmt.executeQuery();
		if(rs.next()){
			studio=new Studio();
			studio.setUserid(rs.getString(1));
		}
		this.pstmt.close();
		return studio;
	}
	public List<Studio> findAll(String keyWord) throws SQLException{
		List<Studio> all=new ArrayList<Studio>();
		String sql="SELECT userid,username FROM studio WHERE userid like ?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, "%"+keyWord+"%");
		ResultSet rs=this.pstmt.executeQuery();
		Studio studio=null;
		while(rs.next()){
			studio=new Studio();
			studio.setUserid(rs.getString(1));
			studio.setUsername(rs.getString(2));
			all.add(studio);
		}
		return all;
		
	}
	
}
